<?php
declare (strict_types=1);

namespace app\api\controller\v1;

use app\common\model\Banner;
use app\common\model\Info as InfoModel;
use app\common\model\Supply;

class Info
{
    //程宇
    //行情资讯
    public function index()
    {
        return sucessMsg('200', '获取成功', [
            'banner' => $this->banner(),//获取banner
            'new1' => $this->new1(),//获取最新资讯上
            'new2' => $this->new2(),//获取最新资讯下左
            'new3' => $this->new3(),//获取最新资讯下右
            'market1' => $this->market1(),//获取行情资讯-国内行情，上
            'market2' => $this->market2(),//获取行情资讯-国内行情，下左
            'market3' => $this->market3(),//获取行情资讯-国内行情，下右
            'market4' => $this->market4(),//获取行情资讯-国内行情，一周排行
            'market5' => $this->market5(),//获取行情资讯-国内行情，全部排行

        ]);
    }

    /**
     * 获取banner
     */
    public function banner()
    {
        $model = New Banner();
        $banner = $model->where(['status' => 1, 'type' => 5])
            ->field('id,title,image')
            ->limit(0, 3)
            ->select();
        foreach ($banner as $v) {
            $v['image'] = env('app.yuming') . $v['image'];
        }
        return $banner;
    }


    /**
     * 获取最新资讯上
     */
    public function new1()
    {
        $model = New InfoModel();
        $new1 = $model->where('status', 1)
            ->field('id,title,content,created_at')
            ->order('id', 'desc')
            ->limit(0, 2)
            ->select();
        foreach ($new1 as $v) {
            $v['created_at'] = date('Y-m-d',strtotime($v['created_at']));
        }
        return $new1;
    }

    /**
     * 获取最新资讯下左
     */
    public function new2()
    {
        $model = New InfoModel();
        $new2 = $model->where('status', 1)
            ->field('id,title')
            ->order('id', 'desc')
            ->limit(2, 6)
            ->select();
        return $new2;
    }

    /**
     * 获取最新资讯下右
     */
    public function new3()
    {
        $model = New InfoModel();
        $new3 = $model->where('status', 1)
            ->field('id,title')
            ->order('id', 'desc')
            ->limit(8, 6)
            ->select();
        return $new3;
    }

    /**
     * 获取行情资讯-国内行情，上
     */
    public function market1()
    {
        $model = New InfoModel();
        $market1 = $model->where(['status' => 1, 'type' => 1])
            ->field('id,title,image,content')
            ->order('id', 'desc')
            ->limit(0, 2)
            ->select();
        foreach ($market1 as $v) {
            $v['image'] = env('app.yuming') . $v['image'];
        }
        return $market1;
    }

    /**
     * 获取行情资讯-国内行情，下左
     */
    public function market2()
    {
        $model = New InfoModel();
        $market2 = $model->where(['status' => 1, 'type' => 1])
            ->field('id,title')
            ->order('id', 'desc')
            ->limit(2, 6)
            ->select();
        return $market2;
    }

    /**
     * 获取行情资讯-国内行情，下右
     */
    public function market3()
    {
        $model = New InfoModel();
        $market3 = $model->where(['status' => 1, 'type' => 1])
            ->field('id,title')
            ->order('id', 'desc')
            ->limit(8, 6)
            ->select();
        return $market3;
    }

    /**
     * 获取行情资讯-国内行情，一周排行
     */
    public function market4()
    {
        $model = New InfoModel();
        $time = time() - 86400;
        $market4 = $model->where(['status' => 1, 'type' => 1])
            ->where('created_at', '>=', $time)
            ->field('id,title')
            ->order('click', 'desc')
            ->limit(0, 15)
            ->select();
        return $market4;
    }


    /**
     * 获取行情资讯-国内行情，全部排行
     */
    public function market5()
    {
        $model = New InfoModel();
        $market5 = $model->where(['status' => 1, 'type' => 1])
            ->field('id,title')
            ->order('click', 'desc')
            ->limit(0, 15)
            ->select();
        return $market5;
    }

    public function but()
    {
        return sucessMsg('200', '获取全部', [
            'local1' => $this->local1(),//获取行情资讯-地方特产，上
            'local2' => $this->local2(),//获取行情资讯-地方特产，下左
            'local3' => $this->local3(),//获取行情资讯-地方特产，下右
            'local4' => $this->local4(),//获取行情资讯-地方特产，一周排行
            'local5' => $this->local5(),//获取行情资讯-地方特产，全部排行
            'list' => $this->list(),//获取相关推荐
        ]);
    }

    /**
     * 获取行情资讯-地方特产，上
     */
    public function local1()
    {
        $model = New InfoModel();
        $local1 = $model->where(['status' => 1, 'type' => 2])
            ->field('id,title,image,content')
            ->order('id', 'desc')
            ->limit(0, 2)
            ->select();
        foreach ($local1 as $v) {
            $v['image'] = env('app.yuming') . $v['image'];
        }
        return $local1;
    }

    /**
     * 获取行情资讯-地方特产，下左
     */
    public function local2()
    {
        $model = New InfoModel();
        $local2 = $model->where(['status' => 1, 'type' => 2])
            ->field('id,title')
            ->order('id', 'desc')
            ->limit(2, 6)
            ->select();
        return $local2;
    }

    /**
     * 获取行情资讯-地方特产，下右
     */
    public function local3()
    {
        $model = New InfoModel();
        $local3 = $model->where(['status' => 1, 'type' => 2])
            ->field('id,title')
            ->order('id', 'desc')
            ->limit(8, 6)
            ->select();
        return $local3;
    }

    /**
     * 获取行情资讯-地方特产，一周排行
     */
    public function local4()
    {
        $model = New InfoModel();
        $time = time() - 86400;
        $local4 = $model->where(['status' => 1, 'type' => 2])
            ->where('created_at', '>=', $time)
            ->field('id,title')
            ->order('click', 'desc')
            ->limit(0, 13)
            ->select();
        return $local4;
    }

    /**
     * 获取行情资讯-地方特产，全部排行
     */
    public function local5()
    {
        $model = New InfoModel();
        $local5 = $model->where(['status' => 1, 'type' => 2])
            ->field('id,title')
            ->order('click', 'desc')
            ->limit(0, 13)
            ->select();
        return $local5;
    }

    /**
     * 获取底部相关推荐
     */
    public function list()
    {
        $model = New Supply();
        $list = $model->where('status', 1)
            ->field('id,title,image,created_at')
            ->order('id', 'desc')
            ->limit(0, 10)
            ->select();
        foreach ($list as $v) {
            $v['image'] = env('app.yuming') . $v['image'];
        }
        return $list;
    }

    //获取详情页
    public function content()
    {
        $id = input('id/d');
        if (empty($id)) {
            return '暂无数据';
        }
        $model = New InfoModel();
        $model->where(['status' => 1, 'id' => $id])
            ->inc('click', 1)
            ->update();
        $content = $model->where(['status' => 1, 'id' => $id])
            ->field('id,created_at,source,click,content,image,title')
            ->find();
        $content['image'] = env('app.yuming') . $content['image'];
        return sucessMsg('200','获取成功',$content);
    }
}
